/**
* <p>Title: FrameController.java</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2015</p>
* <p>Company: SiySoft</p>
* @author liguanghui
* @date 2015年4月29日
* @version 1.0
*/
package com.siysoft.util;

import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.tc.base.controller.QueryParams;


/**
 * <p>Title: BaseUtil</p>
 * <p>Description: </p>
 * <p>Company: dmpb</p>
 * @author    victoryu
 * @date       2015年11月12日
 */



public class BaseUtil {
	
	public void success(HttpServletResponse response, String msg){
		try {
			writerJsonData(response, "{success:true,msg:'" + msg + "'}");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void printSuccess(HttpServletResponse response, String msg,String printTemplate){
		try {
			writerJsonData(response, "{success:true,msg:'" + msg + "',print:'"+printTemplate+"'}");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public  void failure(HttpServletResponse response, String msg){
		try {
			writerJsonData(response, "{success:false,msg:'" + msg + "'}");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 写json数据到前端
	 * @param response
	 * @param json
	 * @throws Exception 
	 */
	
	public void writerJsonData (HttpServletResponse response, Object jsonData) throws Exception{
		Object json = "{}";
		if(jsonData instanceof Map) {
			JSONObject obj = new JSONObject();
			obj.accumulateAll((Map)jsonData);
			json = obj.toString();
		} else if(jsonData instanceof Collection) {
			json = JSONArray.fromObject(jsonData);
		} else {
			json = jsonData;
		}
		response.getWriter().print(json);
	}
	
	
	public QueryParams getQueryParams(HttpServletRequest request) throws Exception{
		request.setCharacterEncoding("UTF-8");
		QueryParams qp=new QueryParams();
		IMap params = new IMap(request);
		String sortField="";
		String sortDir="ASC";
		
		String queryId = params.getString("queryId");
		
		if(!StringUtils.isBlank(queryId)){
			qp.setQueryId(params.getString("queryId"));
		}
		else throw new Exception("queryId is NULL"); 
		
		qp.setPagination(params.getBoolean("isPagination"));
		
		Integer pageSize = Integer.valueOf(0);
		pageSize = params.getInt("pageSize");
		
		if(pageSize<1){
			qp.setPageSize(10);
		}
		else qp.setPageSize(pageSize);
		
		Integer page =  Integer.valueOf(0);
		page=params.getInt("page");
		if(page<1){
			qp.setPage(1);
		}
		else qp.setPage(page);
		
		Integer startIndex = Integer.valueOf(0);
		startIndex = Integer
				.valueOf((qp.getPage() - 1) * qp.getPage());
		qp.setStartIndex(startIndex);
		if(qp.isSort()){
			sortField=params.getString("sort");
			if(!StringUtils.isBlank(sortField)){
				qp.setSortField(sortField);
			}
			else qp.setSort(false);
			
			sortDir=params.getString("dir");
			
			if("DESC".equals(sortDir.toUpperCase())||"ASC".equals(sortDir.toUpperCase())){
				qp.setDir(sortDir);
			}
			else
				qp.setSort(false);
		}
		qp.setParams(params);
		return qp;
		
		
		
	}
	
}
